package com.example.ceshi;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.ceshi.Bean.NowUserData;
import com.example.ceshi.SQLite.MySQLiteOpenHelper;
import com.example.ceshi.utils.DestroyActivityUtil;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private MySQLiteOpenHelper mySQLiteOpenHelper;
    private EditText user,pwd;
    private CheckBox ck_rmb,ck_autoLog;
    private String et_username,et_password;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private Intent intent = new Intent();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        //获取SharedPreferences对象
        sp = getSharedPreferences("users",MODE_PRIVATE);
        mySQLiteOpenHelper=new MySQLiteOpenHelper(LoginActivity.this);
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        setTheme(R.style.Theme_Ceshi3);

        DestroyActivityUtil.addDestoryActivityToMap(this,"LoginActivity");


//        假登录操作
//        mySQLiteOpenHelper.insertUserData("xiaoli","1011");
//        mySQLiteOpenHelper.selectUserByUsername("xiaoli");
        setContentView(R.layout.activity_login);
        user = findViewById(R.id.username);
        pwd = findViewById(R.id.password);
        ck_rmb = findViewById(R.id.rem_me);
        ck_autoLog = findViewById(R.id.auto_login);
        findViewById(R.id.b_login).setOnClickListener(this);
        findViewById(R.id.b_register).setOnClickListener(this);

        //取出数据并渲染
        if(sp.getBoolean("rem",false)){
            user.setText(sp.getString("username",""));
            pwd.setText(sp.getString("password",""));
            ck_rmb.setChecked(true);
            //自动登录判断
            if(sp.getBoolean("auto",false)){
                ck_autoLog.setChecked(true);
                if(mySQLiteOpenHelper.selectUserByUsernameAndPassword(sp.getString("username",""),sp.getString("password",""))){
                    String TAG="111";
                    Log.d(TAG, NowUserData.username+"  "+sp.getString("username",""));
                    intent.setClass(this,MainActivity.class);
                    startActivity(intent);
                    Toast.makeText(LoginActivity.this,sp.getString("username","")+",欢迎您!",Toast.LENGTH_SHORT).show();
                }
            }
        }




    }

    @Override
    public void onClick(View view) {
        et_username = String.valueOf(user.getText());
        et_password = String.valueOf(pwd.getText());
        editor = sp.edit();
        if(view.getId()==R.id.b_login){
//            进行登录操作
            if(mySQLiteOpenHelper.selectUserByUsernameAndPassword(et_username,et_password)){
                //判断是否保存密码
                if(ck_rmb.isChecked()){
                    //保存用户名和密码操作
                    editor.putString("username",et_username);
                    editor.putString("password",et_password);
                    editor.putBoolean("rem",ck_rmb.isChecked());
                }else{
                    editor.clear();
                }

                //判断是否自动登录
                if(ck_autoLog.isChecked()){
                    editor.putBoolean("auto",true);
                }else{
                    editor.putBoolean("auto",false);
                }
                //提交
                editor.commit();
                intent.setClass(this,MainActivity.class);
                startActivity(intent);
                Toast.makeText(LoginActivity.this,et_username+",欢迎您!",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
            }

        }else if(view.getId()==R.id.b_register){
//            进行注册操作
            intent.setClass(this,RegisterActivity.class);
            startActivity(intent);

        }

    }




}